package com.ruoyi.cms.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cms.domain.CmsWebsite;
import com.ruoyi.cms.domain.bo.CmsWebsiteBo;
import com.ruoyi.cms.domain.vo.CmsWebsiteVo;
import com.ruoyi.cms.mapper.CmsWebsiteMapper;
import com.ruoyi.cms.service.ICmsWebsiteService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 站点Service业务层处理
 *
 * @author guorui
 * @date 2023-11-08
 */
@RequiredArgsConstructor
@Service
public class CmsWebsiteServiceImpl implements ICmsWebsiteService {

    private final CmsWebsiteMapper baseMapper;

    /**
     * 查询站点
     */
    @Override
    public CmsWebsiteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询站点列表
     */
    @Override
    public TableDataInfo<CmsWebsiteVo> queryPageList(CmsWebsiteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsWebsite> lqw = buildQueryWrapper(bo);
        Page<CmsWebsiteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询站点列表
     */
    @Override
    public List<CmsWebsiteVo> queryList(CmsWebsiteBo bo) {
        LambdaQueryWrapper<CmsWebsite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsWebsite> buildQueryWrapper(CmsWebsiteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsWebsite> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), CmsWebsite::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), CmsWebsite::getUrl, bo.getUrl());
        return lqw;
    }

    /**
     * 新增站点
     */
    @Override
    public Boolean insertByBo(CmsWebsiteBo bo) {
        CmsWebsite add = BeanUtil.toBean(bo, CmsWebsite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改站点
     */
    @Override
    public Boolean updateByBo(CmsWebsiteBo bo) {
        CmsWebsite update = BeanUtil.toBean(bo, CmsWebsite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsWebsite entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除站点
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public List<CmsWebsiteVo> findAll() {
		return baseMapper.selectVoList(null);
	}
}
